<template>
	<mescroll-uni :ref="'mescrollRef'+i" @init="mescrollInit" @down="downCallback"
		@up="upCallback">
		<view style="padding: 0 40rpx 0 34rpx;">
			<view
			v-for="(item, index) in dataList"
			:key="index"
			class="u-p-t-34 u-p-b-26"
			style="border-bottom: 1rpx solid #EEEEEE;"
			@click="jumpUrl(`/pages/user/personal/extension/detail/detail?activityId=${ item.activityId }`)">
				<view class="u-f-32 u-lh-44 flex-center-between font-medio">
					<view class="linecamp-1">{{ item.title || '' }}</view>
					<view class="clear-flex" :style="{ color: dealStatus(item.status).color }">{{ lang_extension[dealStatus(item.status).key] }}</view>
				</view>
				<view class="u-p-t-14 u-f-26 u-lh-36 linecamp-1">{{ item.subTitle || '' }}</view>
				<view class="u-p-t-12 u-f-24 u-lh-34 color-grew">{{ item.createTime && dayjs(item.createTime).format('YYYY-MM-DD') || '' }}</view>
			</view>
		</view>
	</mescroll-uni>
</template>

<script>
	import { userActivityPage } from '@/api/personal.js'
	import MescrollMixin from "@/components/mescroll-uni/mescroll-mixins.js";
	import MescrollMoreItemMixin from "@/components/mescroll-uni/mixins/mescroll-more-item.js";

	export default {
		mixins: [MescrollMixin, MescrollMoreItemMixin], // 注意此处还需使用MescrollMoreItemMixin (必须写在MescrollMixin后面)
		props: {
			i: Number, // 每个tab页的专属下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
			index: { // 当前tab的下标 (除了支付宝小程序必须在这里定义, 其他平台都可不用写, 因为已在MescrollMoreItemMixin定义)
				type: Number,
				default () {
					return 0
				}
			}
		},
		data() {
			return {
				statusList: [
					{ key: 'unstart', label: '未开始', value: 0, color: '#007AFF' },
					{ key: 'started', label: '已开始', value: 1, color: '#00B578' },
					{ key: 'attended', label: '已参加', value: 2, color: '#00aa00' },
					{ key: 'ended', label: '已结束', value: -1, color: '#FD1E43' },
				],
				dataList: []
			}
		},
		methods: {
			dealStatus(status = 0) {
				let list = this.statusList
				let item = list.find(item => item.value === status) || {}
				return item
			},
			/*上拉加载的回调: 其中page.num:当前页 从1开始, page.size:每页数据条数,默认10 */
			upCallback(page) {
				let node = this.$refs['mescrollRef' + this.i]
				let queryObj = {}
				if (this.i - 1) {
					queryObj.target = this.i - 1
				}
				userActivityPage({
					pageNumber: page.num,
					pageSize: page.size,
					...queryObj
				}).then(res => {
					let dayjs = this.dayjs
					let dv = d => dayjs(d || dayjs()).valueOf();
					let currentTime = dv()
					let list = res.list.map(item => {
						let { startTime, endTime, hasSign } = item
						let status = 0
						if (startTime && currentTime > dv(startTime)) {
							if (endTime && currentTime > dv(endTime)) {
								status = -1
							} else {
								status = 1
							}
						}
						status = hasSign === 1 ? 2 : status
						return ({ ...item, status })
					})
					node && node.mescroll.endSuccess(list.length);
					if (page.num == 1) this.dataList = []; //如果是第一页需手动制空列表
					this.dataList = this.dataList.concat(list); //追加新数据
				}).catch(() => {
					node && node.mescroll.endErr();
				})
			},
			downCallback() {
				let node = this.$refs['mescrollRef' + this.i]
				node && node.mescroll.resetUpScroll()
			},
		}
	}
</script>
